package com.base.chat.ac;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.chat.R;
import com.base.chat.adapter.MyFriendListAdapter;
import com.base.chat.bean.MyFriendListBean;
import com.base.chat.bean.MyFriendsBean;
import com.base.chat.bean.SearchFriendsBean;
import com.base.chat.databinding.FragmentMyFriendsBinding;
import com.base.chat.presenter.SearchFriendsListPresenter;
import com.base.chat.util.ChatStatEntity;
import com.base.chat.view.SearchFriendsListView;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 我的群聊列表
 */
@Route(path = "/chat/my_group")
public class MyGroupChatListActivity extends CBaseBarMvpActivity<SearchFriendsListPresenter> implements SearchFriendsListView {
    private FragmentMyFriendsBinding mBinding;
    private Disposable disposable;
    private MyFriendListAdapter mMyFriendListAdapter;
    private List<MyFriendsBean.Rows> mList=new ArrayList<>();

    @Override
    protected void init() {
        super.init();
        mBinding = (FragmentMyFriendsBinding) mRootBinding;
        mBinding.smartRefreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        mBinding.smartRefreshLayout.setHeaderHeight(60);
        mBinding.smartRefreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        mPresenter =new SearchFriendsListPresenter(this,this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (disposable != null) {
            disposable.dispose();
        }
    }

    @Override
    public void start() {
        mBinding.imgRight.setBackgroundResource(R.mipmap.icon_add_friend);
        mBinding.imgRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/chat/create_group").navigation();
            }
        });
        mBinding.title.setText("群聊");
        mBinding.bar.setNavigationIcon(R.drawable.ic_title_back);
        mBinding.bar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mMyFriendListAdapter= new MyFriendListAdapter(mContext, MyGroupChatListActivity.this,mList);
        mMyFriendListAdapter.isHead(0);
        disposable = RxBus2.getInstance().doSubscribe(ChatStatEntity.class, new Consumer<ChatStatEntity>() {
            @Override
            public void accept(ChatStatEntity bean) throws Exception {
                if (bean.getState() == 0X0000010) {
                    if (mPresenter!=null){
                       // mPresenter.getList(bean.getData());
                    }
                }
            }
        });

        mBinding.rv.setAdapter(mMyFriendListAdapter);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mBinding.rv.setLayoutManager(linearLayoutManager);
//        mBinding.rv.addItemDecoration(new RecyclerViewSpacesItemDecoration
//                (10, 10, 10, 10));
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_my_friends;
    }


    @Override
    public void returnNoData() {

    }

    @Override
    public void returnList(SearchFriendsBean bean) {
     //   mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishRefresh();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
       // mData.clear();
       // mData.addAll(bean.getRows());
       // baseAdapter.notifyDataSetChanged();
    }

    @Override
    public void returnMoreList(SearchFriendsBean bean) {
       // mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
//        mData.addAll(bean
//                .getRows());
//        if (baseAdapter != null) {
//            baseAdapter.notifyDataSetChanged();
//        }
    }

    @Override
    public void returnNoMoreData() {

    }

    int page =1;

    @Override
    public void returnError() {
        //mBinding.empty.setVisibility(View.GONE);
        if (page == 1) {
            mBinding.smartRefreshLayout.finishRefresh();
        } else {
            mBinding.smartRefreshLayout.finishLoadmore();
        }
    }

    @Override
    public void returnAddFriendSuccess(String msg) {
        ToastUtil.show(msg);
    }
}
